import org.javatuples.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@FunctionalInterface
interface Vector2Transformation {
    public Pair<Double, Double> compute(Pair<Double, Double> input, Object arg);
}

public class TupleDemo {
    public static Pair<Double, Double> vector2Compute(Pair<Double, Double> input, Object tArg, Vector2Transformation trans) {
        return trans.compute(input, tArg);
    }

    protected static void pairTupleDemo() {
        // https://www.baeldung.com/java-tuples

//        javatuples 支持10个元素
//
//        Unit<A> (1 element)
//        Pair<A,B> (2 elements)
//        Triplet<A,B,C> (3 elements)
//        Quartet<A,B,C,D> (4 elements)
//        Quintet<A,B,C,D,E> (5 elements)
//        Sextet<A,B,C,D,E,F> (6 elements)
//        Septet<A,B,C,D,E,F,G> (7 elements)
//        Octet<A,B,C,D,E,F,G,H> (8 elements)
//        Ennead<A,B,C,D,E,F,G,H,I> (9 elements)
//        Decade<A,B,C,D,E,F,G,H,I,J> (10 elements)

        Pair<Double, Double> input = Pair.with(1.0, -2.0);

        // 笛卡尔坐标 转 极坐标
        Pair<Double, Double> polarCoord = vector2Compute(input, null, (in, arg)->{
            // 极坐标 r
            // 所有javatuples元组实例都是 不可变的，这个看起来是set方法，但是不改变调用实例数据，只是返回一个新实例
            //rtn.setAt0(Math.sqrt(in.getValue0()*in.getValue0() + in.getValue1()*in.getValue1()));
            double r = Math.sqrt(in.getValue0()*in.getValue0() + in.getValue1()*in.getValue1());
            double a = 0;
            if (0 != r) {
                // 极坐标 角度 1 2 象限
                a = Math.acos(in.getValue0()/r);
                if (in.getValue1() < 0) {
                    // 3 4 象限
                    a = 2*Math.PI - a;
                }
            }
            return Pair.with(r, a);
        });
        System.out.println("笛卡尔坐标 "+input+" 转 极坐标 "+ polarCoord);
    }

    public static Quartet<String, Triplet<Integer, Integer, Integer>, Triplet<Integer, Integer, Integer>, Pair<Integer, String>> makeDateTuple(Date inputDate) {
        // 时区
        // 年,月,日
        // 时,分,秒
        // 一年中的第几周,星期几
        SimpleDateFormat dateFmt = new SimpleDateFormat("z-yyyy-MM-dd-HH-mm-ss-w-E");
        String dateStr = dateFmt.format(inputDate);
        System.out.println("makeDateTuple from dateStr:"+dateStr);

        String[] infoArr = dateStr.split("-");
        if (9 == infoArr.length) {
            return Quartet.with(infoArr[0],
                    Triplet.with(Integer.parseInt(infoArr[1]), Integer.parseInt(infoArr[2]), Integer.parseInt(infoArr[3])),
                    Triplet.with(Integer.parseInt(infoArr[4]), Integer.parseInt(infoArr[5]), Integer.parseInt(infoArr[6])),
                    Pair.with(Integer.parseInt(infoArr[7]), infoArr[8]));
        }
        return null;
    }

    protected static void demoLogic() {
        DateTimeDemo.logTs("pairTupleDemo");
        pairTupleDemo();
        DateTimeDemo.logTs("makeDateTuple");
        System.out.println(makeDateTuple(new Date()));
    }
}
